For...Next

Objectives


Discussion

For...Next Loops

for i = 1 to 10 
   total = total + i
next

for i = 1 to 100
   for j = 1 to 100
      if (i mod j = 0) then 
        sMessage = "WHAT SHOULD THE MESSAGE BE?"
      end if
   Next j
Next i

Isnumeric() Function

Back to top


Demonstration

1.  Create a new project named Unit4.  

2.  Add a textbox (txtNum) and a button (btnSum). 

3.  We will write a function that adds all numbers between 1 and a specified number and returns the sum.   In the code window for the form create the following function.  Make sure you study this function to understand what its doing.

Private Function SumSeries(ByVal iNum As Int16) As Int32
   Dim i As Int16
   Dim total As Int32 = 0
   For i = 1 To iNum
      total = total + i
   Next
   Return total
End Function

4.  Now call this function in the click event for the button.  We will pass the value of txtNum into the function. 

Dim iAnswer As Int32
iAnswer = SumSeries(txtNum.Text)
MessageBox.Show(iAnswer)

5.  Run your program. Enter some numbers.  Also enter some letters. You should get an error because the function is expecting a number.  We can prevent this error by using the built-in IsNumeric() function.  This function accepts a value and determines if the value is a number and returns True or False.  We are also going to add two lines of code to clear the textbox after the user clicks the button and move the cursor back to the textbox.  You should review the code carefully. Change the code in the click event to the following.  Note that the messagebox statement should be on one line.

Dim iAnswer As Int32
If IsNumeric(txtNum.Text) Then
   iAnswer = SumSeries(txtNum.Text)
   MessageBox.Show(iAnswer)
Else
   MessageBox.Show("Enter a number", "Error",  _
     MessageBoxButtons.OK,MessageBoxIcon.Error)
End If
txtNum.Clear()
txtNum.Focus()

6.  Run the program and test it out. 

Back to top
Exercises

1.  Write a for loop to display all numbers from 2 to 50 in the output window using console.writeline.

2. Write a for loop to count down from 100 to 0 by two's while writing the numbers to the output window using console.writeline. The page should display 100, 98, 96, ... , 0.

3.  Write a program that meets the following requirements:

  1. It has a textbox that permits the user to enter a number and a button .
  2. When the user clicks the button, the program checks if the text in the textbox is a number. If it is not a number then the textbox is cleared and focus returns to it.
  3. All even numbers between 0 and the number entered in the textbox are added to a combobox.
  4. All even numbers between 0 and the number are added and displayed on the form in a label.

4.  Create a sub that accepts two numbers.  The  sub should write out a multiplication table to the output window using console.write and console.writeline.  For example if you pass 3 and 5 into the sub the console window should display the following.  The highlighted top row and the first column are multiplied together to get the other numbers in the table.

     1     2     3    4     5

1   1     2     3    4     5

2   2     4     6    8    10

3   3     6     9   12   15

5. Someone makes you the following offer. They will give you either (1) $1,000,000 or (2) the number of pennies that you would have if you start with one penny on day 1 and double the number of pennies every day for 30 days until you reach day 31. Do you take the $1,000,000 or the pennies? Write a program using a for loop to demonstrate which option is better.

Back to top
Links & Help
Back to top